﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NJT.IO
{
    public class 文件
    {
        public static bool 文件合并(List<string> 文件列表, string 目标文件名)
        {
            bool 合并成功;
            var sw = new StreamWriter(目标文件名);
            try
            {
                foreach (var item in 文件列表)
                {
                    var sr = new StreamReader(item, Encoding.Default);
                    sw.Write(sr.ReadToEnd());
                }
                合并成功 = true;
            }
            catch (Exception)
            {
                合并成功 = false;
            }
            finally
            {
                sw.Close();
            }
            return 合并成功;
        }

        /// <summary>
        /// 写入文件s the specified 文件名.
        /// </summary>
        /// <param name="文件名">The 文件名.</param>
        /// <param name="内容">The 内容.</param>
        /// <returns>返回1:写入是否成功  ;返回2:错误信息</returns>
        public static Tuple<bool, string> 写入文件(string 文件名, string 内容)
        {
            try
            {
                var myWriter = new StreamWriter(文件名, false, Encoding.Default);
                myWriter.Write(内容);
                myWriter.Close();
                return new Tuple<bool, string>(true, string.Empty);
            }
            catch (Exception ee)
            {
                return new Tuple<bool, string>(false, ee.Message);
            }
        }

        /// <summary>
        /// 读出文件内容. 
        /// item1 保存结果是否成功
        /// item2 保存错误消息;
        /// item2 保存读取的内容;
        /// </summary>
        /// <param name="文件名">The 文件名.</param>
        /// <returns>返回1:读取是否成功  ;返回2:错误信息;返回3:文件的内容 </returns>
        public static Tuple<bool, string, string> 读出txt(string 文件名)
        {
            string 结果;
            if (文件名.Trim() == "" && !File.Exists(文件名))
            {
                return new Tuple<bool, string, string>(false, "文件不存在", string.Empty);
            }

            StreamReader myReader = null;
            try
            {
                myReader = new StreamReader(文件名, Encoding.Default);
                结果 = myReader.ReadToEnd();
            }
            catch (Exception ee)
            {
                return new Tuple<bool, string, string>(false, ee.Message, string.Empty);
            }
            finally
            {
                if (myReader != null)
                {
                    myReader.Close();
                }
            }
            return new Tuple<bool, string, string>(true, string.Empty, 结果);
        }
    }
}
